import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-05-13
 * Time:11:19
 */
public class UrlPool {
    public static void main(String[] args) {
        getUrl("https://www.nipic.com/");
    }

    private static void getUrl(String url) {
        LinkedHashMap<String, Boolean> map = new LinkedHashMap<>();
        String oldLinkedHost = "";
        Pattern p = Pattern.compile("(https?://)?[^/\\s]*");
        Matcher m = p.matcher(url);
        if (m.find()) {
            oldLinkedHost = m.group();
        }
        map.put(url, false);
        crawlLinks(oldLinkedHost, map);
        for (Map.Entry<String, Boolean> entry : map.entrySet()) {
            System.out.println("链接: " + entry.getKey());
        }
    }

    private static void crawlLinks(String oldLinkedHost, LinkedHashMap<String, Boolean> map) {
        LinkedHashMap<String, Boolean> newMap = new LinkedHashMap<>();
        String link = "";
        for (Map.Entry<String, Boolean> entry : map.entrySet()) {
            if (!entry.getValue()) {
                link = entry.getKey();
                try {
                    URL url = new URL(link);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode() == 200) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                        Pattern p = Pattern.compile("<a.*?href=[\"']?((https?://)?/?[^\"']+)[\"']?.*?>(.+)</a>");
                        Matcher matcher = null;
                        String line = "";
                        while ((line = br.readLine()) != null) {
                            matcher = p.matcher(line);
                            if (matcher.find()) {
                                String newLink = matcher.group(1).trim();
                                if (!newLink.startsWith("http")) {
                                    if (newLink.startsWith("/")) {
                                        newLink = oldLinkedHost + newLink;
                                    } else {
                                        newLink = oldLinkedHost + "/" + newLink;
                                    }
                                }
                                if (newLink.endsWith("/")) {
                                    newLink = newLink.substring(0, newLink.length() - 1);
                                }
                                if (!map.containsKey(newLink)&&
                                        !newMap.containsKey(newLink)&&
                                        newLink.startsWith(oldLinkedHost)){
                                    newMap.put(newLink,false);
                                }
                            }
                        }
                    }
                } catch (Exception e) {

                }
                map.replace(link, false,true);
            }
        }
        if(!newMap.isEmpty()){
            map.putAll(newMap);
            crawlLinks(oldLinkedHost,map);
        }
    }
}
